package com.demo.studentmanager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.studentmanager.dto.AttendanceAddDto;
import com.demo.studentmanager.dto.AttendanceListDto;
import com.demo.studentmanager.dto.AttendanceUpdDto;
import com.demo.studentmanager.entity.Attendance;
import com.demo.studentmanager.result.ResultCode;
import com.demo.studentmanager.service.impl.AttendanceServiceImpl;
import com.demo.studentmanager.service.impl.CourseServiceImpl;
import com.demo.studentmanager.service.impl.StudentServiceImpl;
import com.demo.studentmanager.util.CheckEntityUtil;
import com.demo.studentmanager.vo.AttendanceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;


/**
 * @author lzx
 * 出勤表 attendance controller
 */

@Api(tags = "考勤模块")
@RestController
@RequestMapping("/attendance")
public class AttendanceController {

    @Resource
    private AttendanceServiceImpl attendanceService;

    @Resource
    private StudentServiceImpl studentService;

    @Resource
    private CourseServiceImpl courseService;

    @RequiresAuthentication
    @GetMapping
    @ApiOperation(value = "查询考勤表信息")
    public IPage<AttendanceVo> getStatusList(@Validated AttendanceListDto attendanceListDto) {
        return attendanceService.getAutendanceStatus(attendanceListDto);
    }

    @RequiresRoles(value={"teacher","admin"},logical= Logical.OR)
    @PutMapping
    @ApiOperation(value = "修改考勤状态")
    public void updateStatus(@Validated AttendanceUpdDto attendanceUpdDto) {
        Attendance attendance = CheckEntityUtil.isNullById(attendanceUpdDto.getId(), attendanceService, ResultCode.ATTENDANCE_NOT_EXIST);
        if (attendanceUpdDto.getSno() != null) {
            CheckEntityUtil.isNullById(attendanceUpdDto.getSno(), studentService, ResultCode.STUDENT_NOT_EXIST);
        }
        attendance.setStatus(attendanceUpdDto.getStatus()).setTime(new Date());
        attendanceService.updateById(attendance);

    }

    @RequiresAuthentication
    @PostMapping
    @ApiOperation(value = "新增考勤信息")
    public void addStatus(@Validated AttendanceAddDto attendanceAddDto) {

        CheckEntityUtil.isNotNullById(attendanceAddDto.getId(), attendanceService, ResultCode.ATTENDANCE_EXIST);
        CheckEntityUtil.isNullById(attendanceAddDto.getSno(), studentService, ResultCode.STUDENT_NOT_EXIST);
        CheckEntityUtil.isNullById(attendanceAddDto.getCno(), courseService, ResultCode.COURSE_NOT_EXIST);

        Attendance attendance = new Attendance();
        attendanceAddDto.convertToEntity(attendance);

        attendanceService.save(attendance);
    }

    @RequiresRoles(value={"teacher","admin"},logical= Logical.OR)
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除考勤记录")
    public void deleteStatus(@NotNull @PathVariable String id) {
        CheckEntityUtil.isNullById(id, attendanceService, ResultCode.USER_ACCOUNT_NOT_EXIST);
        attendanceService.removeById(id);
    }


}
